package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.AnswerDetailBo;
import com.tpyule.tpclass.entity.dto.AnswerQueryDto;
import com.tpyule.tpclass.entity.pojo.Answer;
import com.tpyule.tpclass.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/26 21:47
 */
@RestController
@RequestMapping("/answer")
public class AnswerController extends BaseController {
    @Autowired
    AnswerService answerService;
    @RequestMapping("/add")
    @ResponseBody
    public void addAnswer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String answerData = request.getParameter("answerData");
            if (answerData!=null){
                Answer answer = objectMapper.readValue(answerData, Answer.class);
                if (answer!=null){
                    int result = answerService.addAnswer(answer);
                    if (result!=0){
                        resultBean = new SuccessResultBean();
                    } else {
                        resultBean = new FailResultBean();
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

    @RequestMapping("/getListAnswer")
    @ResponseBody
    public void getListAnswer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String answerQueryData = request.getParameter("answerQueryData");
            if (answerQueryData!=null){
                AnswerQueryDto dto = objectMapper.readValue(answerQueryData, AnswerQueryDto.class);
                if (dto!=null){
                    List<AnswerDetailBo> bos = answerService.getListAnswer(dto);
                    if (bos!=null){
                        resultBean = new SuccessResultBean(bos,"成功");
                    } else {
                        resultBean = new FailResultBean();
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }

    @RequestMapping("/updateAnswer")
    @ResponseBody
    public void updateAnswer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String answerData = request.getParameter("answerData");
            if (answerData!=null){
                Answer answer = objectMapper.readValue(answerData, Answer.class);
                if (answer!=null){
                    int result = answerService.updateAnswer(answer);
                    if (result!=0){
                        resultBean = new SuccessResultBean();
                    } else {
                        resultBean = new FailResultBean();
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response, resultBean);
        }
    }
}
